<?php


namespace App\Services;

use App\Jobs\TestTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Swoole\Http\Request;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;

class WebSocketService implements WebSocketHandlerInterface
{
    public function __construct()
    {

    }

    // 连接建立时触发
    public function onOpen(Server $server, Request $request)
    {
        // 在触发 WebSocket 连接建立事件之前，Laravel 应用初始化的生命周期已经结束，你可以在这里获取 Laravel 请求和会话数据
        // 调用 push 方法向客户端推送数据，fd 是客户端连接标识字段
        Log::info('WebSocket 连接建立');
        $nick=$request->get['name'];
        $fd=$request->fd;
        $fds=Redis::get('fds');
        if(empty($fds)){
            $fds=[];
            $fds[]=[
                'fd'=>$fd,
                'nick'=>$nick
            ];
        }else{
            $fds=json_decode($fds,true);
            $fds[]=[
                'fd'=>$fd,
                'nick'=>$nick
            ];
        }
        Redis::set('fds',json_encode($fds));
        $summsg='当前在线人数:'.count($fds).'人';
        foreach ($fds as $key=>$fd){
            $server->push($fd['fd'],json_encode(['mymsg'=>'','notmemsg'=>'','xtmsg'=>$nick.'进入了聊天室','summsg'=>$summsg]));
        }
    }
    // 收到消息时触发
    public function onMessage(Server $server, Frame $frame)
    {   $fds=Redis::get('fds');
        $fds=json_decode($fds,true);
        $summsg='当前在线人数:'.count($fds).'人';
        $data=[
            'renwu'=>'push',
            'data'=>[
                'info'=>['mymsg'=>'','notmemsg'=>'','xtmsg'=>'','summsg'=>$summsg],
                'fd'=>$frame->fd,
                'service'=>$server,
            ]
        ];
        $server=$data['data']['service'];
        $data['data']['info']['notmemsg']='';
        $data['data']['info']['mymsg']=$frame->data;
        //推送给自己
        $server->push($frame->fd,json_encode($data['data']['info']));

        if($frame->data!=''){
            //将信息异步推送给别人
            $data['data']['info']['notmemsg']=$frame->data;
            $data['data']['info']['mymsg']='';
            if(!empty($fds)){
                foreach ($fds as $key=>$fd){
                    if ($fd['fd']!=$data['data']['fd']){
                        $server->push($fd['fd'],json_encode($data['data']['info']));
                    }
                }
            }
        }

//        $task=new TestTask($data);
//        Task::deliver($task);


////        $server->push($frame->fd,$frame->data);
////         调用 push 方法向客户端推送数据
//        if(strpos($frame->data,'redismess')!==false){
//            $info=Redis::get($frame->data);
//            if(empty($info)){
//                $server->push($frame->fd,'');
//            }else{
//                $num=substr($frame->data,strpos($frame->data,'&')+1);
//                $num++;
//                $server->push($frame->fd,$info.'^'.$num);
//            }
////            $server->push($frame->fd, '数据2 This is a message sent from WebSocket Server at ' . date('Y-m-d H:i:s'));
//        }else{
//            $num=substr($frame->data,strpos($frame->data,'&')+1);
//            Redis::set('redismess'.$num,$frame->data);
//            $num++;
//            $server->push($frame->fd, $frame->data.'^'.$num);
//        }
    }

    // 关闭连接时触发
    public function onClose(Server $server, $fd, $reactorId)
    {
        $fds=Redis::get('fds');
        $fds=json_decode($fds,true);
//        $server->push($frame->fd,$frame->fd.'==='.$fds[0]);
        if(!empty($fds)){
            $nick='123';
            foreach ($fds as $key=>$fd1){
                if($fd1['fd']==$fd){
                   $nick=$fd1['nick'];
                    unset($fds[$key]);
                }
            }
            $summsg='当前在线人数:'.count($fds).'人';
            foreach ($fds as $key1=>$fd2){
                $server->push($fd2['fd'],json_encode(['mymsg'=>'','notmemsg'=>'','xtmsg'=>$nick.'离开了聊天室','summsg'=>$summsg]));
            }
            Redis::set('fds',json_encode($fds));
        }
        Log::info('WebSocket 连接关闭');
    }
}